from django.db import models
import time


# Create your models here.
class User(models.Model):
    username = models.CharField(max_length=10)
    password = models.CharField(max_length=12)

    '''
        用户info信息，通过表单添加
        mobile手机号
        email邮箱
        register_time注册时间（自动添加）
        status判断用户是否启用
        rid该用户所属用户组的id
    '''
    mobile = models.CharField(max_length=11, default="")
    email = models.CharField(max_length=30, default="")
    register_time = models.DateTimeField(auto_now_add=True)
    status = models.BooleanField(default=True)
    role = models.ForeignKey(to='Role', on_delete=models.CASCADE)


class Role(models.Model):
    name = models.CharField(max_length=10, verbose_name='角色名')
    direction = models.CharField(max_length=20, verbose_name='角色描述', default='')
    right = models.ManyToManyField(to='Right')


class Right(models.Model):
    name = models.CharField(max_length=10, verbose_name='规则名')
    path = models.CharField(max_length=30, verbose_name='所属路径名')
    level = models.IntegerField(verbose_name='规则等级')
    pid = models.IntegerField(verbose_name='父规则')
    pid_path = models.CharField(max_length=20, verbose_name='父规则列表，包含先祖')


class Category(models.Model):
    name = models.CharField(max_length=15, verbose_name='分类名')
    level = models.IntegerField(verbose_name='分类等级')
    pid = models.IntegerField(verbose_name='父分类id')


class Param(models.Model):
    name = models.CharField(max_length=15, verbose_name='参数名')
    type = models.CharField(max_length=8, verbose_name='参数类型,动态参数dynamic,静态参数static')
    values = models.CharField(max_length=300, verbose_name='参数所有的值，使用<@>分割', default='')
    category = models.ForeignKey(to='Category', on_delete=models.CASCADE)


class Good(models.Model):
    name = models.CharField(max_length=100, verbose_name='商品名')
    price = models.FloatField(verbose_name='商品价格')
    # 由于时间问题该字段直接写在数据库中
    # 更好的做法是将该字段真实内容存储在文件中，将文件路径存储在该字段中
    direction = models.CharField(max_length=4000, verbose_name='商品描述')
    params = models.CharField(max_length=800, verbose_name='商品参数列表，json格式')
    images = models.CharField(max_length=500, verbose_name='商品图片列表，json格式')
    date = models.DateTimeField(auto_now_add=True, verbose_name='创建时间，增加商品时自动添加')

    # 该商品所属分类 与Category绑定
    category = models.ForeignKey(to='Category', on_delete=models.CASCADE)


class Order(models.Model):
    id = models.CharField(max_length=35, primary_key=True, verbose_name='订单编号 字符串形式')
    num = models.IntegerField(verbose_name='购买数量')
    total_pay = models.FloatField(verbose_name='合计支付价钱')
    is_pay = models.BooleanField(verbose_name='是否付款', default=False)
    date = models.DateTimeField(auto_now_add=True, verbose_name='创建时间，创建订单时自动添加')
    express_code = models.CharField(max_length=20, default='', verbose_name='快递单号')
    address = models.CharField(max_length=150, verbose_name='收货地址', default='')

    # 该订单所属商品，商品删除时订单不应该被删除
    good = models.ForeignKey(to='Good', null=True, on_delete=models.SET_NULL)
